package com.itsv.cyzx.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.itsv.cyzx.bean.ContactsBean;
import com.itsv.cyzx.util.CnToSpellUtils;
import com.itsv.cyzx.util.LoadImageAysnc;
import com.itsv.cyzx.util.LoadImageAysnc.ImageCallBack;
import com.itsv.xhyy.R;

public class ContactsJiebieBaseAdapter2 extends BaseAdapter {
	private LayoutInflater mInflater;
	private Context context;
	private ArrayList<ContactsBean> mList;
	private LoadImageAysnc loadImageAysnc;
	private ListView listView;

	public ContactsJiebieBaseAdapter2(Context context,
			ArrayList<ContactsBean> list, ListView listView) {
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mList = list;
		this.listView = listView;
		loadImageAysnc = new LoadImageAysnc(context);
	}

	@Override
	public int getCount() {
		if (mList == null) {
			return 0;
		}
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private boolean isOnly = true;
	private String currentChar;
	private String previewChar;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.from(context).inflate(
					R.layout.contacts_listitem, null);
			holder = new ViewHolder();
			holder.huikorLayout = convertView.findViewById(R.id.huikor_layout);
			holder.mImageButton = (ImageButton) convertView
					.findViewById(R.id.huikorHead);
			holder.fenleiLayout = convertView.findViewById(R.id.fenlei_layout);
			holder.fenleiTextView = (TextView) convertView
					.findViewById(R.id.fenlei_textView);
			holder.mNameTextView = (TextView) convertView
					.findViewById(R.id.huikorName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		int index = position - 1;
		if (index >= 0) {
			ContactsBean bean = mList.get(index);
			if (bean != null) {
				previewChar = mList.get(index).getSubsector();
			}
		} else {
			previewChar = "";
		}
		ContactsBean bean = mList.get(position);
		if (bean != null) {
			currentChar = bean.getSubsector();
		}
		if (!currentChar.equals(previewChar)) {
			holder.fenleiLayout.setVisibility(View.VISIBLE);
			holder.fenleiTextView.setText(mList.get(position).getSubsector());
		} else {
			holder.fenleiLayout.setVisibility(View.GONE);
		}
		// if (!String.valueOf(currentChar).matches("[a-zA-Z]+")) {
		// holder.fenleiLayout.setVisibility(View.VISIBLE);
		// holder.fenleiTextView.setText("#");
		// if (!String.valueOf(currentChar).matches("[a-zA-Z]+")
		// && !String.valueOf(previewChar).trim().equals("") &&
		// !String.valueOf(previewChar).matches("[a-zA-Z]+") ) {
		// holder.fenleiLayout.setVisibility(View.GONE);
		// }
		// } else if (String.valueOf(currentChar).matches("[a-zA-Z]+")) {
		// if (currentChar != previewChar) {
		// holder.fenleiLayout.setVisibility(View.VISIBLE);
		// holder.fenleiTextView.setText(mList.get(position).getJiebie());
		// } else {
		// holder.fenleiLayout.setVisibility(View.GONE);
		// }
		// }
		holder.mImageButton.setTag(mList.get(position).getId());
		Drawable drawable = loadImageAysnc.loadImage(mList.get(position)
				.getId(), mList.get(position).getPicture(), mList.get(position)
				.getId() + ".jpg", new ImageCallBack() {

			@Override
			public void imageLoaded(Drawable drawable, String iconSrc) {
				ImageButton imageViewTag = (ImageButton) listView
						.findViewWithTag(mList.get(position).getId());
				if (imageViewTag != null) {
					imageViewTag.setBackgroundDrawable(drawable);
				}

			}
		});

		if (drawable == null) {
			holder.mImageButton
					.setBackgroundResource(R.drawable.personal_card_avatar_defult);
		} else {
			holder.mImageButton.setBackgroundDrawable(drawable);
		}
		holder.mNameTextView.setText(mList.get(position).getName());
		return convertView;
	}

	class ViewHolder {
		private View huikorLayout;
		private ImageButton mImageButton;
		private View fenleiLayout;
		private TextView fenleiTextView;
		private TextView mNameTextView;
	}
}
